import { Component, Inject, Vue } from "vue-property-decorator";
import { getRandomId } from "@/utils";

@Component
export default class DetailMixin extends Vue {
    @Inject("dataForm") readonly dataForm!: any;
    @Inject("checkShow") private checkShow!: any;
    @Inject("addSelect") private addSelect!: any;

    get dataDetail() {
        return this.dataForm();
    }

    get _onlyData() {
        return !!this.$route.query.onlyData;
    }

    // 获取表单项的值
    getValue(item: any) {
        const { items } = this.dataDetail;
        let res: any = {};
        const cur = items.filter((it: any) => item.id === it.itemId)[0];
        if (["1", "2", "3", "6", "7", "11", "12"].includes(item.valueKindId)) {
            res = {
                value: cur ? cur.itemValue : "",
                id: cur ? cur.dataItemId || getRandomId() : getRandomId()
            };
        } else {
            res = {
                value: cur && cur.data ? cur.data.path || cur.data : [],
                id: cur ? cur.dataItemId || getRandomId() : getRandomId()
            };
        }
        console.log(res);
        return res;
    }

    // 获取子表表单项的值
    getTableValue(colummList: any) {
        const formId = colummList.length ? colummList[0].formId : "";
        const { list } = this.dataDetail;
        const cur = list.filter((it: any) => formId === it.formId);
        const allData = cur[0].data;
        const maxLineNum = Math.max(...allData.map((item: any) => item.lineNum));
        const tableData: any[] = [];
        for (let i = 0; i < maxLineNum; i++) {
            const rowData: any[] = [];
            colummList.forEach((item: any) => {
                const col = allData.filter((it: any) => {
                    return it.itemId === item.id && i + 1 === it.lineNum;
                })[0];
                if (["1", "2", "3", "6", "7", "11", "12"].includes(item.valueKindId)) {
                    rowData.push({
                        ...item,
                        itemValue: col ? col.itemValue : "",
                        dataItemId: col.dataItemId
                    });
                } else {
                    rowData.push({
                        ...item,
                        extData: col && col.data ? col.data.path || col.data : [],
                        dataItemId: col.dataItemId
                    });
                }
            });
            tableData.push(rowData);
        }
        return tableData;
    }

    changeSelectToSow(item: any) {
        const { itemValue: value, options: list } = item;
        if (list && list.length && list[0].id) {
            const option = list.filter((cur: any) => cur.name === value);
            this.addSelect(option[0].id || option[0].tempId, item.id || item.tempId);
        }
    }

    _checkShow(item: any) {
        return this.checkShow(item);
    }
}
